package org.lhh.server.core.codec.trans;

import org.lhh.server.common.utils.CollectionUtils;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.repository.StringObjectId;
import org.pentaho.di.trans.TransDependency;
import org.pentaho.di.trans.TransMeta;

import java.util.ArrayList;
import java.util.List;

public class TransDependencyCodec {
    private String id;
    private String dbName;
    private String tableName;
    private String fieldName;

    public static List<TransDependencyCodec> encode(List<TransDependency> transDependencies) {
        List<TransDependencyCodec> transDependencyCodecs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(transDependencies)) {
            for (TransDependency transDependency : transDependencies) {
                TransDependencyCodec transDependencyCodec = new TransDependencyCodec();
                transDependencyCodec.setId(transDependency.getObjectId().getId());
                transDependencyCodec.setDbName(transDependency.getDatabase().getName());
                transDependencyCodec.setTableName(transDependency.getTablename());
                transDependencyCodec.setFieldName(transDependency.getFieldname());
                transDependencyCodecs.add(transDependencyCodec);
            }
        }
        return transDependencyCodecs;
    }

    public static List<TransDependency> decode(TransMeta transMeta, List<TransDependencyCodec> transDependencyCodecs) {
        List<TransDependency> transDependencies = new ArrayList<>();
        List<DatabaseMeta> databaseMetas = transMeta.getDatabases();
        if (!CollectionUtils.isEmpty(databaseMetas)) {
            if (!CollectionUtils.isEmpty(transDependencyCodecs)) {
                for (TransDependencyCodec transDependencyCodec : transDependencyCodecs) {
                    TransDependency transDependency = new TransDependency();
                    transDependency.setObjectId(new StringObjectId(transDependencyCodec.getId()));
                    DatabaseMeta databaseMeta = transMeta.findDatabase(transDependencyCodec.getDbName());
                    transDependency.setDatabase(databaseMeta);
                    transDependency.setTablename(transDependencyCodec.getTableName());
                    transDependency.setFieldname(transDependencyCodec.getFieldName());
                    transDependencies.add(transDependency);
                }
            }
        }
        return transDependencies;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }
}
